//
// Created by vslab on 23-4-10.
//

#include "util.h"
#include <vector>
#include <string>
#include <map>

using namespace std;

namespace GNSS
{
    Util::Util()
    { }


    void Util::parse_line(string line)
    {
        int start_ = 0;
        int end_ = 0;
        string header;
        msg::msg_type type;
        start_ = line.find(msg::start_flag_);
        end_ = line.find(msg::separator_);
        header = line.substr(start_ + 3, end_ - start_ - 3);
        if (header == "GGA")
        {
            type = msg::GGA;
        }
        else if (header == "GSV")
        {
            type = msg::GSV;
        }
        else if (header == "VTG")
        {
            type = msg::VTG;
        }
        else
        {
            type = msg::NIU;
        }

        switch (type)
        {
            case msg::GGA:
            {
                msg::Gga gga;
                gga.parse(line);
                GGAOfs << gga;
                break;
            }
            case msg::GSV:
            {
                msg::Gsv gsv;
                gsv.parse(line);
                GSVOfs << gsv;
                break;
            }
            case msg::VTG:
            {
                msg::Vtg vtg;
                vtg.parse(line);
                VTGOfs << vtg;
                break;
            }
            case msg::NIU:
                break;
        }
    }

    void Util::parse_file(std::string filename)
    {
        string sep = ",";
        ifstream ifs(filename, ios::out);
        string line;

        string file_header = filename.substr(0, filename.find('.'));
        GGAOfs.open(file_header + "_GGA.csv", ios::app);
        GGAOfs << "Beijing Time" << sep << "Latitude" << sep << "Longitude" << std::endl;
        GSVOfs.open(file_header + "_GSV.csv", ios::app);
        GSVOfs << "Elevation" << sep << "Azimuth" << sep << "SNR" << std::endl;
        VTGOfs.open(file_header + "_VGT.csv", ios::app);
        VTGOfs << "Angle" << sep << "Velocity" << std::endl;
        while(getline(ifs, line))
        {
            parse_line(line);
        }
        GGAOfs.close();
        GSVOfs.close();
        VTGOfs.close();
    }


}